package org.larkdoc.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.larkdoc.dao.ProjectInfoMapper;
import org.larkdoc.model.bo.ProjectBO;
import org.larkdoc.model.entity.ProjectInfo;
import org.larkdoc.model.entity.ProjectRelGroup;
import org.larkdoc.model.entity.ProjectRelUser;
import org.larkdoc.model.vo.PageVO;
import org.larkdoc.model.vo.ProjectVO;
import org.larkdoc.service.ProjectRelGroupService;
import org.larkdoc.service.ProjectRelUserService;
import org.larkdoc.service.ProjectService;
import org.larkdoc.service.UserService;
import org.larkdoc.util.DozerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
/**
 * 项目信息业务功能实现类
 * 
 * @author  zhangpeijun[zhangpeijun1024@qq.com]
 * @version  [v1.0.1, 2017年10月17日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@Service("projectService")
public class ProjectServiceImpl implements ProjectService {
    
    private final static Logger log = LoggerFactory.getLogger(ProjectServiceImpl.class);
    
    @Autowired
    ProjectInfoMapper           projectInfoMapper;
    
    @Autowired
    ProjectRelGroupService      proRelGroupService;
    
    @Autowired
    ProjectRelUserService       proRelUserService;
    
    @Autowired
    UserService                 userService;
    
    @Override
    public int deleteByPrimaryKey(Integer id) {
        // 删除关联关系
        proRelGroupService.deleteByProjectId(id);
        proRelUserService.deleteByProjectId(id);
        return projectInfoMapper.deleteByPrimaryKey(id);
    }
    
    @Override
    public int insert(ProjectInfo record) {
        return projectInfoMapper.insert(record);
    }
    
    @Override
    public int insertSelective(ProjectInfo record) {
        return projectInfoMapper.insertSelective(record);
    }
    
    @Override
    public ProjectInfo selectByPrimaryKey(Integer id) {
        return projectInfoMapper.selectByPrimaryKey(id);
    }
    
    @Override
    public int updateByPrimaryKeySelective(ProjectInfo record) {
        return projectInfoMapper.updateByPrimaryKeySelective(record);
    }
    
    @Override
    public int updateByPrimaryKey(ProjectInfo record) {
        return projectInfoMapper.updateByPrimaryKey(record);
    }
    
    @Override
    public int insertSelective(ProjectBO projectBO) {
        log.debug("ProjectBO insert param : {}", projectBO);
        if (null != projectBO) {
            ProjectInfo project = DozerUtil.conversion(projectBO, ProjectInfo.class);
            int result = insertSelective(project);
            if (result > 0) {
                // 设置ID用于返回给调用者
                projectBO.setId(project.getId());
                int projectId = project.getId();
                List<ProjectRelGroup> groups = projectBO.getProjectRelGroup();
                if (CollectionUtils.isNotEmpty(groups)) {
                    for (ProjectRelGroup projectRelGroup : groups) {
                        projectRelGroup.setProjectId(projectId);
                    }
                    proRelGroupService.insertBatch(groups);
                }
                List<ProjectRelUser> users = projectBO.getProjectRelUser();
                if (CollectionUtils.isNotEmpty(users)) {
                    for (ProjectRelUser projectRelUser : users) {
                        projectRelUser.setProjectId(projectId);
                    }
                    proRelUserService.insertBatch(users);
                }
                projectBO.setProjectRelGroup(groups);
                projectBO.setProjectRelUser(users);
            } else {
                log.info("ProjectBO insert fail. result != 1. ");
            }
            return result;
        } else {
            log.info("ProjectBO insert param is null.");
            return 0;
        }
    }
    
    @Override
    public int updateByPrimaryKeySelective(ProjectBO projectBO) {
        log.debug("ProjectBO updateByPrimaryKeySelective param : {}", projectBO);
        if (null != projectBO) {
            ProjectInfo project = DozerUtil.conversion(projectBO, ProjectInfo.class);
            int result = updateByPrimaryKeySelective(project);
            if (result == 1) {
                int projectId = project.getId();
                proRelGroupService.deleteByProjectId(projectId);
                proRelGroupService.insertBatch(projectBO.getProjectRelGroup());
                proRelUserService.deleteByProjectId(projectId);
                proRelUserService.insertBatch(projectBO.getProjectRelUser());
            } else {
                log.info("ProjectBO updateByPrimaryKeySelective fail. result != 1. ");
            }
            return result;
        } else {
            log.info("ProjectBO updateByPrimaryKeySelective param is null.");
            return 0;
        }
    }
    
    @Override
    public PageInfo<ProjectBO> page(PageVO<ProjectVO> record) {
        record = null == record ? new PageVO<ProjectVO>() : record;
        PageHelper.startPage(record.getPageNum(), record.getPageSize());
        return new PageInfo<ProjectBO>(projectInfoMapper.selectByProjectVO(record.getSearch()));
    }
    
    @Override
    public List<ProjectInfo> selectByProjectInfo(ProjectVO record) {
        // 如果参数为null则查询所有数据。
        return projectInfoMapper.selectByProjectInfo(record);
    }
    
    @Override
    public int deleteByIdsAndAccount(int[] ids, String account) {
        if (null != ids && ids.length > 0 && StringUtils.isNotBlank(account)) {
            return projectInfoMapper.deleteByIdsAndAccount(ids, account);
        } else {
            return 0;
        }
    }
    
    @Override
    public List<Integer> filterIds(List<Integer> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            return projectInfoMapper.filterIds(ids);
        } else {
            return null;
        }
    }
    
    @Override
    public List<ProjectInfo> selectByDocIds(List<Integer> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            return projectInfoMapper.selectByDocIds(ids);
        } else {
            return null;
        }
    }
    
    @Override
    public List<Integer> selectIdsByUser(Integer userId, List<Integer> idList, String creator) {
        if(userId > 0) {
            return projectInfoMapper.selectIdsByUser(userId, idList, creator);
        } else {
            return null;
        }
    }
    
}